id/email
password
forgot password | create account
about | help | prefs
ReadingBatcode reading practice

 

 

For LoopsForLoop2

prev  |  next  |  chance

The .substring() method returns a portion of a string.

public static int loopIt(String str) {
    int result = 0;

    for (int i = 0; i < str.length(); i++) {
        if (str.substring(i, i + 1).equals("a"))
            result++;
    }

    return result;
}
Function Call  Return Value
loopIt("athenian")
loopIt("apples")
loopIt("hello")
loopIt("")
loopIt(" ")
loopIt("alphabet")

Experiment with this code on Gitpod.io

⬅ Back